// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023.
 * Description: Propagate choice of HMAC algorithm in evm crypto
 * Author: zhangguangzhi
 * Create: 2023-11-6
 */

#include <crypto/hash_info.h>
#include <linux/kstrtox.h>
#include <linux/init.h>
#include <linux/errno.h>
#include "evm.h"
#include "evm_crypto_hash_setup.h"

static int hash_setup_done;
static int __init hash_setup(char *str)
{
	int i;

	i = match_string(hash_algo_name, HASH_ALGO__LAST, str);
	if (i < 0) {
		pr_err("invalid hash algorithm \"%s\"", str);
		return 1;
	}

	evm_hash_algo = i;
	hash_setup_done = 1;
	return 1;
}
__setup("evm_hash=", hash_setup);

/*
 * Configure the hash algorithm for the HMAC
 */
int __init evm_init_crypto(void)
{
	int i;

	if (hash_setup_done)
		return 0;

	i = match_string(hash_algo_name, HASH_ALGO__LAST,
			 CONFIG_EVM_DEFAULT_HASH);

	if (i < 0) {
		pr_err("invalid hash algorithm \"%s\"",
		       CONFIG_EVM_DEFAULT_HASH);
		pr_info("Error initializing crypto\n");
		return -EINVAL;
	}

	evm_hash_algo = i;

	return 0;
}
